#include "custompanel.h"

CustomPanel::CustomPanel(QWidget *parent) : QWidget(parent)
{
    this->initControl();
    this->initForm();
    this->initMenu();
    this->show_client_all();
}

CustomPanel::~CustomPanel()
{

}

QSize CustomPanel::sizeHint() const
{
    return QSize(800, 600);
}

QSize CustomPanel::minimumSizeHint() const
{
    return QSize(80, 60);
}

bool CustomPanel::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonDblClick) {
        QLabel *widget = (QLabel *) watched;
        if(!clientMax) {
            clientMax = true;
            hide_client_all();
            gridLayout->addWidget(widget, 0, 0);
            widget->setVisible(true);
        } else {
            clientMax = false;
            show_client_all();
        }
    } else if(event->type() == QEvent::MouseButtonPress) {
        QMouseEvent *mouseEvent = (QMouseEvent *)event;
        if(mouseEvent->button() == Qt::RightButton) {
            clientMenu->exec(QCursor::pos());
        }
    }
    return QWidget::eventFilter(watched, event);
}

void CustomPanel::initControl()
{
    gridLayout = new QGridLayout;
    gridLayout->setSpacing(1);
    gridLayout->setContentsMargins(0, 0, 0, 0);
    gridLayout->setObjectName("gridLayout");
    this->setLayout(gridLayout);
}

void CustomPanel::initForm()
{
    //设置样式表
#ifndef no_style
    QStringList qss;
    qss.append("QFrame{border:2px solid #000000;}");
    qss.append("QLabel{font:75 25px;color:#F0F0F0;border:2px solid #AAAAAA;background:#303030;}");
    qss.append("QLabel:focus{border:2px solid #00BB9E;background:#555555;}");
    this->setStyleSheet(qss.join(""));
#endif

    clientMax = false;
    clientCount = 16;
    clientType = "1_16";
    for(int i = 0; i < clientCount; i++) {
        QLabel *widget = new QLabel;
        widget->setObjectName(QString("client%1").arg(i+1));
        widget->installEventFilter(this);
        widget->setFocusPolicy(Qt::StrongFocus);
        widget->setAlignment(Qt::AlignCenter);

        widget->setText(QString("客户端:%1").arg(i+1));
        clientWidget.append(widget);
    }
}

void CustomPanel::initMenu()
{
    clientMenu = new QMenu(this);

    actionFull = new QAction("切换全屏模式", clientMenu);
    connect(actionFull, SIGNAL(triggered(bool)), this, SLOT(full()));
    actionPoll = new QAction("启动轮询视频", clientMenu);
    connect(actionPoll, SIGNAL(triggered(bool)), this, SLOT(poll()));

    clientMenu->addAction(actionFull);
    clientMenu->addAction(actionPoll);
    clientMenu->addSeparator();

    clientMenu->addAction("截图当前视频", this, SLOT(snapshot_client_one()));
    clientMenu->addAction("截图所有视频", this, SLOT(snapshot_client_all()));
    clientMenu->addSeparator();

    QMenu *menu4 = clientMenu->addMenu("切换到4画面");
    menu4->addAction("通道1-通道4", this, SLOT(show_client_4()));
    menu4->addAction("通道5-通道8", this, SLOT(show_client_4()));
    menu4->addAction("通道9-通道12", this, SLOT(show_client_4()));
    menu4->addAction("通道13-通道16", this, SLOT(show_client_4()));

    clientMenu->addAction("切换到16画面", this, SLOT(show_client_16()));
}

void CustomPanel::hide_client_all()
{
    for(int i = 0; i < clientCount; i++) {
        gridLayout->removeWidget(clientWidget.at(i));
        clientWidget.at(i)->setVisible(false);
    }
}

void CustomPanel::change_client(int index, int flag)
{
    int count = 0;
    int row = 0;
    int column = 0;

    //行列数一致的比如 2*2 3*4 4*4 5*5 等可以直接套用通用的公式
    //按照这个函数还可以非常容易的拓展出 10*10 16*16=256 通道界面
    for (int i = 0; i < clientCount; i++) {
        if (i >= index) {
            gridLayout->addWidget(clientWidget.at(i), row, column);
            clientWidget.at(i)->setVisible(true);

            count++;
            column++;
            if (column == flag) {
                row++;
                column = 0;
            }
        }

        if (count == (flag * flag)) {
            break;
        }
    }
}

void CustomPanel::change_client_4(int index)
{
    hide_client_all();
    change_client(index, 2);
}

void CustomPanel::change_client_16(int index)
{
    hide_client_all();
    change_client(index, 4);
}

void CustomPanel::full()
{
    if (actionFull->text() == "切换全屏模式") {
        emit fullScreen(true);
        actionFull->setText("切换正常模式");
    } else {
        emit fullScreen(false);
        actionFull->setText("切换全屏模式");
    }
}

void CustomPanel::poll()
{

}

void CustomPanel::snapshot_client_one()
{

}

void CustomPanel::snapshot_client_all()
{

}

void CustomPanel::show_client_all()
{
    if(clientType == "1_4") {
        change_client_4(0);
    } else if(clientType == "5_8") {
        change_client_4(4);
    } else if(clientType == "9_12") {
        change_client_4(8);
    } else if(clientType == "3_16") {
        change_client_4(12);
    } else if(clientType == "1_16") {
        change_client_16(0);
    }
}

void CustomPanel::show_client_4()
{
    QAction *action = (QAction *)sender();
    QString name = action->text();

    int index = 0;
    QString clientType;
    if (name == "通道1-通道4") {
        index = 0;
        clientType = "1_4";
    } else if (name == "通道5-通道8") {
        index = 4;
        clientType = "5_8";
    } else if (name == "通道9-通道12") {
        index = 8;
        clientType = "9_12";
    } else if (name == "通道13-通道16") {
        index = 12;
        clientType = "13_16";
    }

    if (this->clientType != clientType) {
        this->clientType = clientType;
        this->clientMax = false;
        change_client_4(index);
    }
}

void CustomPanel::show_client_16()
{
    int index = 0;
    QString videoType = "1_16";
    if (this->clientType != videoType) {
        this->clientType = videoType;
        this->clientMax = false;
        change_client_16(index);
    }
}



